package org.despair2D.ui.core 
{
	import flash.display.Sprite;
	import flash.geom.Point;
	import org.despair2D.renderer.IView;
	import org.despair2D.ui.events.InteractiveEvent;
	import org.despair2D.ui.events.VisibleEvent;
	
	import org.despair2D.core.ns_despair;
	use namespace ns_despair;
	
	/** @inheritDoc **/
public class SpriteAdvance extends Sprite implements IView
{
	
	/** 交互失效 **/
	ns_despair static const c_mouseDisabledFlag:uint  =  0x01
	
	/** 不可见 **/
	ns_despair static const c_invisibleFlag:uint      =  0x02
	
	/** 滤镜 **/
	ns_despair static const c_filterFlag:uint         =  0x04
	
	
	final public function get globalPos() : Point
	{
		m_cachedPoint.x = this.x;
		m_cachedPoint.y = this.y;
		return this.parent.localToGlobal(m_cachedPoint);
	}
	
	override public function get rotation():Number{ return m_rotation; }
	override public function set rotation( v:Number ) : void 
	{
		super.rotation = m_rotation = Boolean(v % 360 < 0) ? ( v % 360 + 360 ) : ( v % 360 );
	}
	
	override public function get scaleX():Number{ return this.m_scaleX; }
	override public function set scaleX( v:Number ) : void 
	{
		super.scaleX = m_scaleX = (v < 0) ? 0 : v;
	}
	
	override public function get scaleY():Number { return this.m_scaleY; }
	override public function set scaleY( v:Number ) : void
	{ 
		super.scaleY = m_scaleY = (v < 0) ? 0 : v;
	}
	
	override public function get alpha() : Number{ return m_alpha; }
	override public function set alpha( v:Number ) : void 
	{
		super.alpha = m_alpha = v;
	}
	
	override public function set filters( v:Array ) : void
	{
		super.filters = v;
		if (v)
		{
			m_flags |= c_filterFlag;
		}
		else
		{
			m_flags &= ~c_filterFlag;
		}
	}
	
	override public function get visible() : Boolean{ return !Boolean(m_flags & c_invisibleFlag) }
	override public function set visible( b:Boolean ) : void 
	{
		super.visible = b;
		if (b)
		{
			m_flags &= ~c_invisibleFlag;
		}
		else
		{
			m_flags |= c_invisibleFlag;
		}
		this.dispatchEvent(new VisibleEvent(VisibleEvent.VISIBLE_CHANGE, b))
	}
	
	public function get interactive() : Boolean { return !Boolean(m_flags & c_mouseDisabledFlag) }
	public function set interactive( b:Boolean ) : void
	{
		if (b)
		{
			m_flags &= ~c_mouseDisabledFlag;
		}
		else
		{
			m_flags |= c_mouseDisabledFlag;
		}
		this.dispatchEvent(new InteractiveEvent(InteractiveEvent.INTERACTIVE_CHANGE, b));
	}
	
	
	public function move( x:Number, y:Number ) : void
	{
		this.x = x;
		this.y = y;
	}	
	
	final public function moveInStage( x:Number, y:Number ) : void
	{
		var local:Point
		
		m_cachedPoint.x  =  x
		m_cachedPoint.y  =  y
		local            =  this.parent.globalToLocal(m_cachedPoint)
		this.x           =  local.x
		this.y           =  local.y
	}
	
	public function offset( tx:Number, ty:Number ) : void
	{
		this.x += tx;
		this.y += ty;
	}
	
	public function reset() : void
	{
		if (m_flags & c_invisibleFlag)
		{
			super.visible = true;
			m_flags &= ~c_invisibleFlag;
		}
		
		if (m_flags & c_filterFlag)
		{
			super.filters = null
			m_flags &= ~c_filterFlag;
		}
		
		if (m_alpha != 1 || m_rotation != 0 || m_scaleX != 1 || m_scaleY != 1)
		{
			super.rotation = m_rotation = 0;
			super.scaleX = m_scaleX = super.scaleY = m_scaleY = super.alpha = m_alpha = 1;
		}
		
		this.x = this.y = 0;
	}
	
	ns_despair function dispose() : void
	{
		if (this.parent)
		{
			this.parent.removeChild(this);
		}
		
		if (m_flags & c_mouseDisabledFlag)
		{
			m_flags &= ~c_mouseDisabledFlag;
		}
		
		this.reset()
	}
	
	
	ns_despair static var m_cachedPoint:Point = new Point()
	
	
	ns_despair var m_flags:uint
	
	ns_despair var m_alpha:Number = 1;

	ns_despair var m_scaleX:Number = 1;
	
	ns_despair var m_scaleY:Number = 1;
	
	ns_despair var m_rotation:Number = 0;	
}
}